import requests
import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))


# 主函数
if __name__ == "__main__":
    # url = "http://127.0.0.1:5000/send_image"
    # while True:
    #     input_content = input('输入图片路径，输入-1退出，默认值os.path.join(BASE_DIR, "data", "global-wheat-detection", "test", "51b3e36ab.jpg") ')
    #     if input_content.strip() == "":
    #         input_content = 'D:\\PycharmWorkPlaces\\DeepModel_deploy_flask\\data\\global-wheat-detection\\test\\51b3e36ab.jpg'
    #     if input_content.strip() == "-1":
    #         break
    #     elif not os.path.exists(input_content.strip()):
    #         print('输入图片路径不正确，请重新输入')
    #     else:
    #         imageFilePath = input_content.strip()
    #         imageFileName = os.path.split(imageFilePath)[1]
    #         file_dict = {
    #             'file':(imageFileName,
    #                 open(imageFilePath,'rb'),
    #                 'image/jpg')}
    #         result = requests.post(url, files=file_dict)
    #         predict_result = result.text
    #         print('图片路径:%s 预测结果为:%s\n' %(imageFilePath, predict_result))

    url = "http://127.0.0.1:5000/send_image"
    imageFilePath01 = './static/received_images/in000474.jpg'
    imageFileName01 = os.path.split(imageFilePath01.strip())[1]
    imageFilePath02 = './static/received_images/in001700.jpg'
    imageFileName02 = os.path.split(imageFilePath02.strip())[1]
    file_dict = {
        'img01':(imageFileName01,
                open(imageFilePath01,'rb'),
                'image/jpg'),
        'img02' : (imageFileName02 ,
               open(imageFilePath02, 'rb') ,
               'image/jpg') ,
    }
    result = requests.post(url, files=file_dict)
    print(dir(result))
    print(dir(result.content))


    img = result.content  # mp3二进制数据

    # 将mp3的二进制数据保存到本地的mp3
    f = open("./static/client_output/binary.jpg" , "wb")
    f.write(img)
    f.close()

    # print(result.content)
    # predict_result = result.text
    # print('图片路径:%s 预测结果为:%s\n' %(imageFilePath, predict_result))
